package controller;

import aop.RequiredPermission;
import com.github.pagehelper.PageInfo;
import entity.Admin;
import entity.LayRequest;
import entity.MyResult;
import entity.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.RoleService;

import java.util.List;

@Controller
@RequestMapping("/role")//设定该类中所有请求的统一访问前缀
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequestMapping("/add")
    public String add(Role role){
        System.out.println("进入控制层方法");
        boolean flag=roleService.insert(role);
        //重定向 ”redirect:重定向的请求地址“ 通常重定向到controller方法
        return "redirect:list";//重定向到list请求
    }
    @RequiredPermission("role:add")
    @ResponseBody
    @RequestMapping("/add2")
    public MyResult add2(Role role){
        System.out.println("进入控制层方法");
        boolean flag=roleService.insert(role);
        //重定向 ”redirect:重定向的请求地址“ 通常重定向到controller方法
        return new MyResult(0);
    }

    @RequestMapping("/toAdd")
    public  String toAdd(){
        return "add.jsp";
    }

    @RequiredPermission("role:list")
    @RequestMapping("/list")
    public String list(ModelMap modelMap){
        //查询所有角色
        List<Role> roles=roleService.selectAll();
        //将角色集合传递到前台页面
        modelMap.put("roles",roles);
        return "list.jsp";//抓发到对应的页面
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        boolean flag=roleService.delete(id);
        return "redirect:list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,ModelMap modelMap){
        //查询角色信息
        Role role=roleService.selectById(id);
        //将角色信息传递到页面
        modelMap.put("role",role);
        return "update.jsp";

    }
    @RequiredPermission("role:update")
    @RequestMapping("/update")
    public String update(Role role){
        roleService.update(role);
        return "redirect:list";
    }

    @ResponseBody
    @RequestMapping("/update2")
    public MyResult update2(Role role){
        roleService.update(role);
        return new MyResult(0);
    }

    @RequiredPermission("role:list")
    @ResponseBody
    @RequestMapping("/laylist")
    public MyResult<List<Role>> layList(LayRequest layRequest){
        //执行分页查询
        PageInfo<Role> pageInfo=roleService.selectByPage(layRequest);
        return new MyResult<>(0,pageInfo.getList(),pageInfo.getTotal());
    }

    @ResponseBody
    @PostMapping("/batchDelete")
    public MyResult batchDelete(Integer[] ids){
        roleService.batchDelete(ids);
        return new MyResult(0);
    }
}
